//
//  COUserInfoManager.swift
//  CocoProject
//
//  Created by Vill on 2023/11/6.
//

import UIKit

class COUserInfoManager: NSObject {
    
    static let shared = COUserInfoManager()
    
    var curUser:COUserInfoResponseModel?
    
    var gifts:COGiftSearchModel?
    
    // MARK: - 归档路径设置
    private static var savePath: String {
        return "COUserInfoJsonData"
    }
    
    private static var saveGiftPath: String {
        return "COGiftsData"
    }
    
    static func coUserLogin() -> Bool {
        var login = false
        let json = UserDefaults.standard.object(forKey: COUserInfoManager.savePath) as? String
        let user = JSONDeserializer<COUserInfoResponseModel>.deserializeFrom(json: json)
        if user?.token.count ?? 0 > 0{
            login = true
        }
        return login
    }
    
    static func coSaveInfo(){
        let json = COUserInfoManager.shared.curUser?.toJSONString()
        UserDefaults.standard.set(json, forKey: COUserInfoManager.savePath)
        UserDefaults.standard.synchronize()
    }
    
    static func coGetInfo(){
        let json = UserDefaults.standard.object(forKey: COUserInfoManager.savePath) as? String
         let user = JSONDeserializer<COUserInfoResponseModel>.deserializeFrom(json: json)
        if let user = user {
            COUserInfoManager.shared.curUser = user
        }
    }
    
    static func coClearInfo(){
        COUserInfoManager.shared.curUser = nil
        UserDefaults.standard.removeObject(forKey: COUserInfoManager.savePath)
        UserDefaults.standard.synchronize()
    }

    static func coRouteToTabbar() {
        KAppDelegate.window?.rootViewController = COBaseTabbarController()
    }
    
    static func coRouteToLogin() {
        DispatchQueue.main.async(execute: {
            let vc = COGuestController.init()
            let rootVC = UINavigationController(rootViewController: vc)
            KAppDelegate.window?.rootViewController = rootVC
        })
    }
    
    static func configLoginData(response:COUserInfoResponseModel?) {
        if let _ = response{
            COUserInfoManager.shared.curUser = response
            COUserInfoManager.coSaveInfo()
        }
    }
    
    // 礼物模型已经加载
    static func giftsLoaded() -> Bool {
        var load = false
        let json = UserDefaults.standard.object(forKey: COUserInfoManager.saveGiftPath) as? String
        let user = JSONDeserializer<COGiftSearchModel>.deserializeFrom(json: json)
        if user?.gift.count ?? 0 > 0{
            load = true
        }
        return load
    }
    
    // 存储礼物模型
    static func saveGiftsInfo(){
        let json = COUserInfoManager.shared.gifts?.toJSONString()
        UserDefaults.standard.set(json, forKey: COUserInfoManager.saveGiftPath)
        UserDefaults.standard.synchronize()
    }
    
    // 获取礼物模型
    static func getGiftsInfo(){
        let json = UserDefaults.standard.object(forKey:COUserInfoManager.saveGiftPath) as? String
         let giftmodel = JSONDeserializer<COGiftSearchModel>.deserializeFrom(json: json)
         if let model = giftmodel {
             COUserInfoManager.shared.gifts = model
         }
    }
    
    //清除礼物模型
    static func clearGiftsInfo(){
        COUserInfoManager.shared.gifts = nil
        UserDefaults.standard.removeObject(forKey: COUserInfoManager.saveGiftPath)
        UserDefaults.standard.synchronize()
    }
    
    static func configGiftsData(response:COGiftSearchModel?) {
        if let _ = response{
            COUserInfoManager.shared.gifts = response
            COUserInfoManager.saveGiftsInfo()
        }
    }

}
